package com.ocean.redflagtest.mvvm.views.ui

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.Display
import android.view.View
import android.view.WindowManager

import com.ocean.redflagtest.utils.OceanUtils
import com.ocean.redflagtest.mvvm.models.entity.CircleModel


/**
 * Created by ocean on 2018/8/1
 * Author :  ocean
 * Email  :  348686686@qq.com
 */
class OceanCircleView : View {

    private var mContext: Context? = null

    /**
     * 绘制圆body，填白
     */
    private var mPaintCircle: Paint? = null

    /**
     * 描边，红
     */
    private var mPaintCircleStrok: Paint? = null

    /**
     * 绘制文字
     */
    private var mPaintCircleText: Paint? = null

    /**
     * 绘制图形
     */
    private var mPaintCircleBitmap: Paint? = null


    /**
     * 绘制数据
     */
    var datas: List<CircleModel>? = null


    /**
     * 屏幕宽度
     */
    private var screenWidth: Int = 0

    /**
     * 屏幕高度
     */
    private var screenHeight: Int = 0

    /**
     * 单位转换率
     */
    private var unit: Float = 0.toFloat()


    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    private fun init(context: Context) {
        mContext = context

        mPaintCircle = Paint()
        mPaintCircle!!.color = Color.WHITE
        mPaintCircle!!.style = Paint.Style.FILL

        mPaintCircleStrok = Paint()
        mPaintCircleStrok!!.color = Color.RED
        mPaintCircleStrok!!.style = Paint.Style.STROKE
        mPaintCircleStrok!!.strokeWidth = 3f

        mPaintCircleText = Paint()
        mPaintCircleText!!.color = Color.RED
        mPaintCircleText!!.strokeWidth = 2f
        mPaintCircleText!!.textSize = 40f
        mPaintCircleText!!.textAlign = Paint.Align.CENTER

        mPaintCircleBitmap = Paint()
        mPaintCircleBitmap!!.color = Color.RED

        val windowManager = mContext!!.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = windowManager.defaultDisplay
        screenWidth = display.width
        screenHeight = display.height

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        unit = (screenWidth / 750.0).toFloat()
        OceanUtils.logE("屏幕宽度 ->" + screenWidth + "屏幕高度 ->" + screenHeight)
        OceanUtils.logE("缩放比例：$unit")

        if (datas != null && !datas!!.isEmpty()) {
            for (circleModel in datas!!) {
                drawCircle(canvas, circleModel)
                drawCircleStrok(canvas, circleModel)
                drawCircleText(canvas, circleModel, circleModel?.text.toString())
                drawCircleBitmap(canvas, circleModel,
                        BitmapFactory.decodeResource(mContext!!.resources, circleModel.imgResource))

            }
        }

    }

    private fun drawCircle(canvas: Canvas, model: CircleModel) {
        val size = model.circleRadius
        canvas.drawCircle((model.circleLeft + size) * unit,
                (model.circleTop + size) * unit,
                model.circleRadius * unit,
                mPaintCircle!!)
    }

    private fun drawCircleStrok(canvas: Canvas, model: CircleModel) {
        val size = model.circleRadius
        canvas.drawCircle((model.circleLeft + size) * unit,
                (model.circleTop + size) * unit,
                model.circleRadius * unit,
                mPaintCircleStrok!!)
    }

    private fun drawCircleText(canvas: Canvas, model: CircleModel, text: String) {
        val fontMetrics = mPaintCircleText!!.fontMetrics
        val top = fontMetrics.top//为基线到字体上边框的距离,即上图中的top
        val bottom = fontMetrics.bottom//为基线到字体下边框的距离,即上图中的bottom

        val baseLineY = ((model.circleTop + model.circleRadius) * unit - top / 2 - bottom / 2).toInt()//基线中间点的y轴计算公式
        canvas.drawText(text,
                (model.circleLeft + model.circleRadius) * unit,
                (baseLineY + 30).toFloat(),
                mPaintCircleText!!)
    }

    private fun drawCircleBitmap(canvas: Canvas, model: CircleModel, bitmap: Bitmap) {
        val size = model.circleRadius
        canvas.drawBitmap(Bitmap.createScaledBitmap(bitmap, 60, 60, true),
                (model.circleLeft + size) * unit - 30,
                (model.circleTop + size) * unit - 60,
                mPaintCircleBitmap)
    }


    fun startDraw() {
        if (datas != null && !datas!!.isEmpty())
            postInvalidate()
    }

}
